package com.googlecode.perfectable.visuable.message.client;

import com.googlecode.perfectable.eventable.routing.Sink;
import com.googlecode.perfectable.prosperable.message.ClientMessage;
import com.googlecode.perfectable.visuable.message.ValueChangeMessage;
import com.googlecode.perfectable.visuable.visual.ValueHoldingVisual;

public class ValueChangeMessageHandler implements Sink<ClientMessage> {
	private final ValueHoldingVisual target;

	private ValueChangeMessageHandler(ValueHoldingVisual target) {
		this.target = target;
	}

	public static ValueChangeMessageHandler create(ValueHoldingVisual target) {
		return new ValueChangeMessageHandler(target);
	}

	@Override
	public void receive(ClientMessage message) {
		if(!(message instanceof ValueChangeMessage)) {
			return;
		}
		ValueChangeMessage buttonPushMessage = (ValueChangeMessage) message;
		if(buttonPushMessage.isDirectedAt(this.target)) { // MARK performIfTargeted?
			buttonPushMessage.perform(this.target);
		}
	}

}
